//
//  CedarTabBar.m
//  StarOrangeStaff
//
//  Created by lshenrong on 2020/6/22.
//  Copyright © 2020 lshenrong. All rights reserved.
//

#import "CedarTabBar.h"

@implementation CedarTabBar



- (instancetype)init {
    self = [super init];
    if (self) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *image = [UIImage imageNamed:@"Home"];
        [btn setImage:image forState:UIControlStateNormal];
        btn.bounds = CGRectMake(0, 0, 70, 68);
        self.centerBtn = btn;
        [self addSubview:btn];
    }
    return self;
}



- (void)layoutSubviews{
    
    [super layoutSubviews];
    BOOL isiPhoneX = NO;
    CGFloat offset = 0;
    if (IsPhoneX) {
        isiPhoneX = YES;
        offset = 12;
    };
    self.centerBtn.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.3 - offset);
    
    int index = 0;
    CGFloat wigth = self.bounds.size.width / 3;

    for (UITabBarItem *item in self.items) {
        if ([item isKindOfClass:NSClassFromString(@"UITabBarItem")]) {
            [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIFont systemFontOfSize:14], NSFontAttributeName, nil]
                                forState:UIControlStateNormal];
        }
        if (isiPhoneX) {
            item.titlePositionAdjustment =  UIOffsetMake(0, -20);
        }
    }
    
    for (UIView* sub in self.subviews) {
        if ([sub isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
         
            sub.frame = CGRectMake(index * wigth, self.bounds.origin.y - offset, wigth, (self.bounds.size.height - 2) );
            index++;
            
            if (index == 1) {
                index++;
            }
        }
    }
}


- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (self.isHidden == NO) {
        CGPoint newPoint = [self convertPoint:point toView:self.centerBtn];
        if ( [self.centerBtn pointInside:newPoint withEvent:event]) {
            return self.centerBtn;
        }else{
            return [super hitTest:point withEvent:event];
        }
    }else {
        return [super hitTest:point withEvent:event];
    }
}


@end
